﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace IanUtil.Serialization
{
    public class IU_SL_XML
    {
        public static object XML2Object(string xml, Type type)
        {
            MemoryStream ms = new MemoryStream();
            StreamWriter sw = new StreamWriter(ms);
            sw.Write(xml);
            sw.Flush();
            ms.Position = 0;
            XmlSerializer xs = new XmlSerializer(type);
            object obj = xs.Deserialize(ms);
            sw.Close();
            return obj;
        }

        public static string Object2XML(object obj)
        {
            MemoryStream ms = new MemoryStream();
            XmlSerializer xs = new XmlSerializer(obj.GetType());
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            xs.Serialize(ms, obj, ns);
            ms.Position = 0;
            StreamReader sr = new StreamReader(ms);
            string xml = sr.ReadToEnd();
            sr.Close();
            return xml;
        }
    }
}
